//
//  NYPublishViewController.m
//  BS
//
//  Created by neal on 15/8/20.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYPublishViewController.h"
#import "NYPublishButton.h"
#import "NYNavigationController.h"
#import "NYPostViewController.h"

#import <POP.h>

@interface NYPublishViewController ()
/* 动画时间 */
@property (nonatomic,strong)  NSArray *animatimes;
/* 所有按钮 */
@property (nonatomic,strong)  NSMutableArray *buttons;
/* 标签 */
@property (nonatomic,strong)  UIImageView *sloganView;

@end

@implementation NYPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupPublishButton];
    
    [self setupsloganView];
}

- (NSMutableArray *)buttons
{
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}
//关闭界面
- (void)closeWithTask:(void(^)())task
{
    self.view.userInteractionEnabled = NO;
    for (int i = 0; i < _buttons.count; i ++) {
        UIButton *button = self.buttons[i];
        
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
        CGFloat buttonCenterX = button.centerX;
        CGFloat buttonCenterY = button.centerY + NYScreenH;
        CGPoint center = CGPointMake(buttonCenterX, buttonCenterY);
        anim.toValue = [NSValue valueWithCGPoint:center];
        anim.beginTime = CACurrentMediaTime() + [self.animatimes[i] floatValue];
        [button pop_addAnimation:anim forKey:nil];
        
    }
    POPBasicAnimation *sloanim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
    CGFloat sloCenterX = self.sloganView.centerX;
    CGFloat sloCenterY = self.sloganView.centerY + NYScreenH;
    CGPoint slocenter =  CGPointMake(sloCenterX, sloCenterY);
    sloanim.toValue = [NSValue valueWithCGPoint:slocenter];
    sloanim.beginTime = CACurrentMediaTime() + [[self.animatimes lastObject] floatValue];
    [self.sloganView pop_addAnimation:sloanim forKey:nil];
    
    [sloanim setCompletionBlock:^(POPAnimation *popanimation, BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
        }];
        
        if (task) {
            task();
        }
    }];

    
}
- (IBAction)cancel {
    [self closeWithTask:nil];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self closeWithTask:nil];
}
- (void)setupsloganView
{
    /**
     设置标语
     */
    UIImageView *sloganView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
    sloganView.centerX = NYScreenW * 0.5;
    sloganView.centerY = - NYScreenH;
    [self.view addSubview:sloganView];
    self.sloganView = sloganView;
    CGFloat sloganViewcenterY = NYScreenH * 0.15;
    CGFloat sloganViewcenterX = NYScreenW * 0.5;
    
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(sloganViewcenterX, sloganViewcenterY)];
    /**
     *  设置弹性系数
     */
    anim.springBounciness = 10;
    anim.springSpeed = 10;
    anim.beginTime = CACurrentMediaTime() + [[self.animatimes lastObject] floatValue];
    [sloganView pop_addAnimation:anim forKey:nil];
}
- (void)setupPublishButton
{
    NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
    NSArray *titles = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"];    self.animatimes = @[@0.1,@0.2,@0.6,@0.5,@0.4,@0.3,@0.8];

    NSUInteger totolColsCount = 3;
    NSUInteger rows = (images.count + totolColsCount - 1)/totolColsCount;
    CGFloat buttonW = NYScreenW / totolColsCount;
    CGFloat buttonH = buttonW;
    CGFloat orginalButtonY = (NYScreenH - rows * buttonH) * 0.5;

    for (int i = 0; i <images.count ; i++) {
        UIButton *button = [[NYPublishButton alloc]init];
        button.tag = i;
        [self.view addSubview:button];
        [self.buttons addObject:button];
        
        CGFloat buttonX = (i % totolColsCount) * buttonW;
        CGFloat buttonY = - NYScreenH;
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
    
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(publishbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        POPSpringAnimation *springanima = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        buttonY = (i / totolColsCount) * buttonH + orginalButtonY;
        springanima.toValue = [NSValue valueWithCGRect:CGRectMake(button.x, buttonY, buttonW, buttonH)];
        springanima.beginTime = CACurrentMediaTime() +[_animatimes[i] floatValue];
        [button pop_addAnimation:springanima forKey:nil];
    }
}
- (void)publishbuttonClick:(UIButton *)button
{
    [self closeWithTask:^{
        if (button.tag == 2) {
            NYPostViewController *vc = [[NYPostViewController alloc]init];
            NYNavigationController *nav = [[NYNavigationController alloc]initWithRootViewController:vc];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
        } else {

            NYLog(@"%@",button.currentTitle);
        }
    }];

}
@end
